package com.hnust.root.web.controller.forward.user;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONObject;
import com.hnust.root.common.base.exception.BusinessException;
import com.hnust.root.common.base.response.BaseRestController;
import com.hnust.root.common.base.response.BaseResult;
import com.hnust.root.dao.entity.domain.forward.WxUserInfo;
import com.hnust.root.service.forword.wx.WxUserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/group/user")
public class WxAccountController extends BaseRestController {
    @Autowired
    private WxUserInfoService wxUserInfoService;

    @ApiOperation("获取用户详细信息")
    @PostMapping("/getUserInfo")
    public BaseResult<WxUserInfo> getWxUserInfo() {
        String openId = getWxAccountOpenId();
        WxUserInfo userInfo = wxUserInfoService.getUserInfoByOpenId(openId);
        return fillSuccessData(userInfo);
    }


    @ApiOperation("更新用户详细信息")
    @PostMapping("/updateUserInfo")
    public BaseResult<WxUserInfo> updateUserInfo(@RequestBody JSONObject userInfo) throws BusinessException {
        String openId = getWxAccountOpenId();
        String key = userInfo.getStr("type");
        String value = userInfo.getStr("value");
        boolean result = wxUserInfoService.updateUserInfo(openId, key, value);
        return result ? fillSuccessData() : fillFailData();
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/updateUser")
    public BaseResult<WxUserInfo> updateUser(@RequestBody WxUserInfo userInfo) throws BusinessException {
        String openId = getWxAccountOpenId();
        boolean result = wxUserInfoService.updateUser(userInfo, openId);
        return result ? fillSuccessData() : fillFailData();
    }

    @ApiOperation("根据OpenId获取用户详细信息")
    @PostMapping("/getSelectUserInfo")
    public BaseResult<WxUserInfo> getSelectUserInfo(@RequestBody JSONObject jsonObject) {
        String queryOpenId = jsonObject.getStr("userOpenId");
        WxUserInfo userInfo = wxUserInfoService.getUserInfoByOpenId(queryOpenId);
        return fillSuccessData(userInfo);
    }

    @ApiOperation("获取我的校友")
    @GetMapping("/schoolmates")
    public BaseResult<List<WxUserInfo>> getMySchoolMates() {
        String openId = getWxAccountOpenId();
        return fillSuccessData(wxUserInfoService.getSchoolMates(openId));
    }
}
